package com.lanyou.cook.service;

import java.io.IOException;

import javax.annotation.PostConstruct;

import org.apache.curator.framework.recipes.queue.SimpleDistributedQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.vo.InterfaceExecutionParam;

/*
 * 向执行器发送【执行接口】消息
 */

@Service
public class KettleJobSender {
	@Autowired
	private CuratorService curatorService;
	
	private SimpleDistributedQueue queue;
	
	@PostConstruct
	private void init() {
		if(!curatorService.isEnable())
			return;
		queue = new SimpleDistributedQueue(curatorService.getClient(), Constants.ZOOKEPPER_PATH_TO_DO_JOB);
	}
	
	public boolean send(InterfaceExecutionParam param){
		if(param == null || (param.getKettleJobId() <= 0) )
			return false;
		if(queue == null)
			return false;
		
		byte[] rawData = param.serialize();
		
		if(!curatorService.isConnected()){
			return false;
		}
		
		try {
			queue.offer(rawData);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
